package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.util.DateUtils;
import com.easemob.util.TextFormater;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.VideoEntity;
import com.example.zbh.dsliao.utils.video.ImageResizer;
import com.example.zbh.dsliao.view.RecyclingImageView;

import java.util.List;

/**
 * Created by zbh on 15/11/28.
 */
public class VideoAdapter extends BaseAdapter {
    private List<VideoEntity> list;
    private int itemHeight =0;
    private RelativeLayout.LayoutParams params;
    private ImageResizer imageResizer;
    public VideoAdapter() {

        params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
    public void addData(List<VideoEntity> list, ImageResizer imageResizer){
        this.list = list;
        this.imageResizer = imageResizer;
        notifyDataSetChanged();
    }


    public boolean isLength(){
        return list != null && list.size() > 0;
    }
    @Override
    public int getCount() {
        return isLength() ? list.size() + 1 : 1;
    }

    @Override
    public Object getItem(int position) {
        return (position == 0) ? null : list.get(position -1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_video_item, null);
            holder.imageView = (RecyclingImageView) convertView.findViewById(R.id.imageView);
            holder.icon = (ImageView) convertView.findViewById(R.id.video_icon);
            holder.tvDur = (TextView) convertView.findViewById(R.id.chatting_length_iv);
            holder.tvSize = (TextView) convertView.findViewById(R.id.chatting_size_iv);
            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.imageView.setLayoutParams(params);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (holder.imageView.getLayoutParams().height !=itemHeight ){
            holder.imageView.setLayoutParams(params);
        }
        String str = "拍摄录像";
        if (position == 0){
            holder.icon.setVisibility(View.GONE);
            holder.tvDur.setVisibility(View.GONE);
            holder.tvSize.setText(str);
            holder.imageView.setImageResource(R.mipmap.dl_actionbar_camera_icon);
        }else {
            holder.icon.setVisibility(View.VISIBLE);
            VideoEntity video = list.get(position -1);
            holder.tvDur.setVisibility(View.VISIBLE);
            holder.tvDur.setText(DateUtils.toTime(video.duration));
            holder.tvSize.setText(TextFormater.getDataSize(video.size));
            holder.imageView.setImageResource(R.mipmap.dl_empty_photo);
            imageResizer.loadImage(video.filePath, holder.imageView);

        }

        return convertView;
    }
    public void setItemHeight(int height){
        if(height == itemHeight){
            return;
        }
        itemHeight = height;
        params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight);
        imageResizer.setImageSize(height);
        notifyDataSetChanged();

    }
    class ViewHolder{
        RecyclingImageView imageView;
        ImageView icon;
        TextView tvDur;
        TextView tvSize;
    }
}
